package com.example.joewang.repluginmotion.hook;

import android.content.Intent;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;


public class IActivityManagerHook implements InvocationHandler {

    private Object instance;

    public IActivityManagerHook(Object IActivityManagerInstance) {
        instance = IActivityManagerInstance;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if ("startActivity".equals(method.getName())) {
            int index = -1;
            for (int i = 0; i < args.length; i++) {
                if (args[i] instanceof Intent) {
                    index = i;
                    break;
                }
            }
            Intent intent = new Intent();
            Intent originIntent = (Intent) args[index];
            String packageName = "com.example.joewang.repluginmotion";
            intent.setClassName(packageName, packageName + ".ProxyActivity");
            intent.putExtra(HookUtils.HOOK_ORIGIN_INTENT, originIntent);
            args[index] = intent;
        }
        return method.invoke(instance, args);
    }
}
